import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import alias from '@rollup/plugin-alias';
import copyFile from './copy-file';
import { obsidianExportPath } from './env';

const isProd = process.env.BUILD === 'production';

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
if you want to view the source visit the plugins github repository
*/
`;

export default {
  input: './src/main.ts',
  output: {
    dir: '.',
    sourcemap: 'inline',
    sourcemapExcludeSources: isProd,
    format: 'cjs',
    exports: 'default',
    banner,
  },
  external: ['obsidian'],
  plugins: [
    alias({
      entries: {
        '@app': 'src',
        '@lib': 'src/lib',
      },
    }),
    typescript(),
    nodeResolve({ browser: true }),
    commonjs(),
    copyFile({
      targets: [
        { src: './main.js', dest: obsidianExportPath },
        { src: './manifest.json', dest: obsidianExportPath },
        { src: './src/styles.css', dest: obsidianExportPath },
      ],
    }),
  ],
  onwarn: (warning) => {
    if (warning.code === 'THIS_IS_UNDEFINED') return;

    console.warn(warning.message);
  },
};
